package com.example.compose

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Button
import androidx.compose.material3.Card
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.contentColorFor
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import androidx.compose.ui.window.DialogProperties
import com.google.accompanist.permissions.ExperimentalPermissionsApi
import com.google.accompanist.permissions.isGranted
import com.google.accompanist.permissions.rememberPermissionState
import com.google.accompanist.permissions.shouldShowRationale

@OptIn(ExperimentalPermissionsApi::class)
@Preview
@Composable
private fun FeatureThatRequiresCameraPermission() {

    var openMinimalDialog by remember { mutableStateOf(false) }
    // Camera permission state
    val cameraPermissionState = rememberPermissionState(
        android.Manifest.permission.CAMERA
    )

    if (cameraPermissionState.status.isGranted) {
        Text("Camera permission Granted")
    } else {
        Column {
            val shouldShowRationale = cameraPermissionState.status.shouldShowRationale
            when (shouldShowRationale) {
                true -> {
                    MinimalDialog1(onDismissRequest = { openMinimalDialog = true })
                }

                else -> {}
            }
            val textToShow = if (shouldShowRationale) {
                // If the user has denied the permission but the rationale can be shown,
                // then gently explain why the app requires this permission
                "The camera is important for this app. Please grant the permission."
            } else {
                // If it's the first time the user lands on this feature, or the user
                // doesn't want to be asked again for this permission, explain that the
                // permission is required
                "Camera permission required for this feature to be available. " +
                        "Please grant the permission"
            }
            Text(textToShow)
            Button(onClick = { cameraPermissionState.launchPermissionRequest() }) {
                Text("Request permission")
            }
        }
    }
}

@Composable
fun MinimalDialog1(onDismissRequest: () -> Unit) {
    Dialog(
        onDismissRequest = { onDismissRequest() },
        properties = DialogProperties(
            usePlatformDefaultWidth = false,
            dismissOnBackPress = true,
        ),
    ) {
        Card(
            modifier = Modifier
                .fillMaxWidth()
                .height(200.dp)
                .padding(16.dp),
            shape = RoundedCornerShape(16.dp),
        ) {
            Text(
                text = "This is a minimal dialog",
                modifier = Modifier
                    .fillMaxSize(),
//                    .wrapContentSize(Alignment.Center),
                textAlign = TextAlign.Center,
            )
            Button(onClick = { }) {
                Text(text = "确定")
            }
        }
    }
}

@OptIn(ExperimentalPermissionsApi::class)
@Preview
@Composable
fun Sample() {
    val cameraPermissionState =
        rememberPermissionState(permission = android.Manifest.permission.CAMERA)
    if (cameraPermissionState.status.isGranted) {
        Text(text = "已授权相机权限")
    } else {
        var showDialog by remember {
            mutableStateOf(false)
        }
        Button(onClick = { showDialog = true }) {
            Text(text = "拍照")
        }
        if (showDialog) {
            AnimatedDialog(onDismiss = { showDialog = false }) {
                Column(
                    modifier = Modifier
                        .fillMaxWidth()
                        .background(
                            color = MaterialTheme.colorScheme.surface,
                            shape = RoundedCornerShape(24.dp)
                        )
                        .padding(24.dp)
                ) {
                    Text(
                        text = "拍照",
                        color = MaterialTheme.colorScheme.contentColorFor(MaterialTheme.colorScheme.surface),
                        style = MaterialTheme.typography.titleLarge
                    )
                    Spacer(modifier = Modifier.height(8.dp))

                    Text(
                        text = "文字摄像头对此应用程序非常重要。请授予许可",
                        color = MaterialTheme.colorScheme.contentColorFor(MaterialTheme.colorScheme.surface),
                        style = MaterialTheme.typography.titleLarge
                    )
                    Spacer(modifier = Modifier.height(8.dp))
                    Button(
                        onClick = { cameraPermissionState.launchPermissionRequest() },
                        modifier = Modifier.align(Alignment.End)
                    ) {
                        Text(text = "确定")
                    }
                }
            }
        }
    }
//        Column {
//            val textToShow = if (cameraPermissionState.status.shouldShowRationale) {
//                "文字摄像头对此应用程序非常重要。请授予许可。"
//            } else {
//                "相机不可用."
//            }
//            Text(text = textToShow)
//            Spacer(modifier = Modifier.height(8.dp))
//            Button(onClick = { cameraPermissionState.launchPermissionRequest() }) {
//                Text(text = "请求权限")
//            }
//        }
}